import { ref } from 'vue'
import { taskAddApi, taskDeleteApi } from '@/api/ReviewBusiness/01Project/project'
import useInstance from '@/hooks/useInstance';
import { EditType } from '@/type/BaseEnum'
import {useRouter} from 'vue-router'
export default function ProjectAddEdit(getTableList, searchParams, ProjectData) {
  const { global, proxy } = useInstance()
  const router = useRouter()
  const RefTaskProject = ref<{ show: (type: string, row?: any) => void }>()
  const addBtn = () => {
    RefTaskProject?.value?.show(EditType.ADD)  
  }
  const editBtn = (row) => {
    RefTaskProject?.value?.show(EditType.EDIT, row)
  }
  const deleteBtn = async (id) => {
    const confirm = await global.$myconfirm('确定删除该任务嘛?')
    if (confirm) {
      const res = await taskDeleteApi({ id })
      if (res.code === 200) {
        global.$message({ message: '删除成功', type: 'success' })
        if (ProjectData.list.length === 1 && searchParams.page > 1) {
          searchParams.page -= 1
          getTableList()
        } else {
          getTableList()
        }
      }
    } else {
      global.$message({ message: "已取消删除", type: 'warning' })
    }
  }
  const resetBtn = (id) => { }
  const serachBtn = (id) => {
    searchParams.page = 1
    getTableList()
  }
  const save = async (row) => {
    const res = await taskAddApi(row)
    if (res.code === 200) {
      global.$message({ message: '新增成功', type: 'success' })
      searchParams.page = 1
      getTableList()
    }
  }
  const detailLook = async (row) => { 
    router.push({
      path:'/02ReviewBusinessMan/01projectManagement/AuditHomePage',
      query:{
        id:row.id,
        name:row.name,
        // taskBaseline:row.taskBaseline
        // taskBaseline:1
      }
    })
  }
  return {
    RefTaskProject, addBtn, editBtn, deleteBtn, resetBtn, serachBtn, save, detailLook
  }
}